package license

import (
	"context"

	v1 "gitee.com/liangziyisheng/caas-platform-mc/api/license/v1"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/consts"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/dao"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/service"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
)

type sLicense struct{}

var l = g.Log("logger for License")

func New() *sLicense {
	return &sLicense{}
}

func init() {
	l.SetFlags(glog.F_FILE_LONG)
	service.RegisterLicense(New())
}
func (s *sLicense) My(ctx context.Context, in *v1.MyInput) (out *v1.MyOutput, err error) {
	out = &v1.MyOutput{}
	md := dao.License.Ctx(ctx).Where(dao.License.Columns().TenantCode, in.TenantCode)
	if in.Status != 0 {
		md = md.Where(dao.License.Columns().Status, in.Status)
	}
	err = md.Scan(&out.Data)
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(consts.ListFailed)
	}
	return out, nil
}
func (s *sLicense) List(ctx context.Context, in *v1.ListInput) (out *v1.ListOutput, err error) {
	out = &v1.ListOutput{}
	md := dao.License.Ctx(ctx)

	if in.AppCode != "" {
		md = md.Where(dao.License.Columns().AppCode, in.AppCode)
	}
	// 判断是否是平台管理员,管理员能查询所有许可
	if in.TenantCode != "" {
		md = md.Where(dao.License.Columns().TenantCode, in.TenantCode)
	}
	if in.Status != 0 {
		md = md.Where(dao.License.Columns().Status, in.Status)
	}
	err = md.Limit((in.Page-1)*in.Size, in.Size).ScanAndCount(&out.Data, &out.Total, true)
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.ListFailed, in))
	}

	return out, nil
}

func (s *sLicense) Add(ctx context.Context, in *v1.AddInput) (out *v1.AddOutput, err error) {
	// 增加许可
	id, err := dao.License.Ctx(ctx).InsertAndGetId(in.License)
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.AddFailed, in.License))
	}
	if id == 0 {
		return nil, gerror.NewCode(gcode.WithCode(consts.AddFailed, in.License))
	}
	return &v1.AddOutput{Id: id}, nil
}

func (s *sLicense) Delete(ctx context.Context, in *v1.DeleteInput) (out *v1.DeleteOutput, err error) {
	// 先确定许可已经失活
	cls := dao.License.Columns()
	md := dao.License.Ctx(ctx).Where(cls.Id, in.Id)
	rows, err := md.Where(cls.Status, consts.LicenseStatus[1].Value).Count()
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.QueryLicenseFailed, in.Id))
	}
	if rows > 0 {
		return nil, gerror.NewCode(gcode.WithCode(consts.LicenseIsActive, in.Id))
	}
	// 最后删除
	md = dao.License.Ctx(ctx).Where(cls.Id, in.Id)
	rs, err := md.Delete()
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.DeleteFailed, in.Id))
	}
	out = &v1.DeleteOutput{}
	if out.Rows, err = rs.RowsAffected(); err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.DeleteFailed, in.Id))
	}
	if out.Rows == 0 {
		return nil, gerror.NewCode(gcode.WithCode(consts.DeleteFailed, in.Id))
	}
	return out, nil
}

func (s *sLicense) Update(ctx context.Context, in *v1.UpdateInput) (out *v1.UpdateOutput, err error) {
	out = &v1.UpdateOutput{}
	cls := dao.License.Columns()
	md := dao.License.Ctx(ctx).Data(in.License).Where(cls.Id, in.Id)
	// isPlatformAdmin := ctx.Value(consts.TokenClaims.PlatformAdmin).(bool)
	// if !isPlatformAdmin {
	// 	md = md.Where(dao.License.Columns().TenantCode, in.TenantCode)
	// }
	out.Rows, err = md.UpdateAndGetAffected()
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.UpdateFailed, in.License))
	}
	if out.Rows == 0 {
		return nil, gerror.NewCode(gcode.WithCode(consts.UpdateFailed, in.License))
	}
	return out, nil
}
